package com.duan.common.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class PachongUtil {

	private static String fileName = "sjz";
	private static String writeFile = "/Users/xygame/Documents/";

//	public static void main(String[] args) throws MalformedURLException, IOException, InterruptedException {
//
//
//		//获取列表数据
//		for (int i = 0; i<50; i++) {
//			String url = "https://" + fileName + ".meituan.com/meishi/pn" + (i+1) + "/";
//			System.out.println(LocalDateTime.now() + "---------开始爬取数据：{ " + url + " }---------------");
//			//获取列表
//			List<String> sellerIdList = getList(httpRequest(url));
//			if (sellerIdList.size()<=0) {
//				System.out.println(LocalDateTime.now() + "---------执行当前城市数据获取完毕---------------");
//				break;
//			}
//			int j = 0;
//			for (String sellerId : sellerIdList) {
//				j++;
//				boolean isReConnect = true;
//				while (isReConnect) {
//					String detailUrl = "http://www.meituan.com/meishi/" + sellerId + "/";
//					try {
//						StringBuilder sb = getDetail(httpRequest(detailUrl));
//						writeFile(sb.toString(), fileName + ".txt");
//						int waitTime = ((int)(Math.random() * 10));
//						System.out.println(LocalDateTime.now() + "第：" + j + "次 连接：" + detailUrl + " 写入数据到文件中成功.等待：" + waitTime + " 秒, 写入的数据为：" + sb.toString());
//						TimeUnit.SECONDS.sleep(waitTime);
//						isReConnect = false;
//					} catch (ConnectException e) {
//						System.out.println("--------当前网络断开 3分钟后重新连接------------");
//						isReConnect = true;
//						TimeUnit.MINUTES.sleep(3);
//					} catch (IllegalArgumentException e) {
//						System.out.println("--------当前页面需要输入验证码打开美团页面" + detailUrl + "，输入验证码, 3分钟后重新访问该连接------------");
//						isReConnect = true;
//						TimeUnit.MINUTES.sleep(3);
//					} catch (Exception e) {
//						e.printStackTrace();
//						System.err.println("------------未知的异常,3分钟后重试---------------");
//						isReConnect = true;
//						TimeUnit.MINUTES.sleep(3);
//					}
//				}
//			}
//			int pageWaitTime = (int)(Math.random() * 3);
//			System.out.println(LocalDateTime.now() + " 页面:" + url + " 列表页面数据写入到磁盘完毕.等待：" + pageWaitTime + " 分钟");
//			TimeUnit.MINUTES.sleep(pageWaitTime);
//		}
//
//	}

	private static Document httpRequest(String url) throws IOException {
		Map<String,String> map = new HashMap<>();
		map.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
		map.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
		map.put("Cookie", "uuid=4496e9e4a21cee0a536d.1538994481.1.0.0; _lxsdk_cuid=16653382c299f-0e91220a917bcd-346a7808-fa000-16653382c2ac8; __mta=213799851.1538994486024.1538994486024.1538994486024.1; client-id=851936d4-3441-498e-92f3-c15eaedc9345; IJSESSIONID=y3klz8fse5m5jfc7zd00wt4; iuuid=7859FB9EB7F3F29BAC8CAD3350D852F84A77A5F3C24C3AC0DF123F6BE4470D0B; cityname=%E9%9D%92%E5%B2%9B; _lxsdk=7859FB9EB7F3F29BAC8CAD3350D852F84A77A5F3C24C3AC0DF123F6BE4470D0B; webp=1; __utma=74597006.1726245832.1539154198.1539154198.1539154198.1; __utmc=74597006; __utmz=74597006.1539154198.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); latlng=31.18826,121.43687,1539154222426; i_extend=C197856950957682894427581510064063813598_b0_e9014270906189264598_v1049915013615683502_a%e7%be%8e%e9%a3%9f_f177949557H__a; _hc.v=3bcdaefb-3a4a-a14d-1060-f5376822e75c.1539154317; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; ci=10; rvct=10%2C60%2C45; lat=29.554065; lng=106.572632; _lxsdk_s=%7C%7C0");
		map.put("Accept-Encoding", "gzip, deflate");
		map.put("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
		map.put("Cache-Control", "max-age=0");
		map.put("Connection", "keep-alive");

		Document document = Jsoup.connect(url).headers(map).get();
		//验证是否输入验证码
		checkVerify(document, url);
		return document;
	}


	public static void main(String[] args) throws IOException {
		String url = "https://www.meituan.com/changecity/";
		getCityLink(httpRequest(url));
	}

	public static void getCityLink (Document document) {
		Elements allCityElements = document.getElementsByClass("cities");
		int z = 0;
		for (int i = 0; i < allCityElements.size(); i++) {
			//获取字母城市信息
			Element cityElement = allCityElements.get(i);
//			System.out.println(cityElement);
			Elements elementsTagA = cityElement.getElementsByTag("a");
			for (int j = 0; j < elementsTagA.size(); j++) {
				System.out.print("url[" + z++ + "]: https:" + elementsTagA.get(j).attr("href"));
				System.out.print("," + elementsTagA.get(j).ownText());
//				System.out.println("," + j);
			}
		}
	}


	public static List<String> getList(Document document) {
		Elements elements = document.getElementsByTag("script");
		Integer maxSize = 10000;
		List<String> sellerList = new ArrayList<String>();
		for (int i = 0; i<elements.size() && i < maxSize; i++) {
			Element element = elements.get(i);
			if (element.data() != null && element.data().contains("window._appState")) {
				String jsonStr = element.data().toString().replaceAll("window._appState = ", "").replace(";", "");
				JSONObject jsonObject = JSON.parseObject(jsonStr);
				JSONObject contentJsonObject = jsonObject.getJSONObject("poiLists");
				int totalCount = contentJsonObject.getInteger("totalCounts");
				System.out.println("总数量为：" + totalCount);
				JSONArray contentJsonArray = contentJsonObject.getJSONArray("poiInfos");
				for (int j = 0; j < contentJsonArray.size(); j++) {
					JSONObject contentJsonObj = contentJsonArray.getJSONObject(j);
//					System.out.print(", poiId: " + contentJsonObj.getString("poiId"));
//					System.out.print(", title: " + contentJsonObj.getString("title"));
//					System.out.print(", avgScore: " + contentJsonObj.getString("avgScore"));
//					System.out.print(", allCommentNum: " + contentJsonObj.getString("allCommentNum"));
//					System.out.print(", address: " + contentJsonObj.getString("address"));
//					System.out.println(", avgPrice: " + contentJsonObj.getString("avgPrice"));
					sellerList.add(contentJsonObj.getString("poiId"));
				}
			}
		}
		return sellerList;
	}



	public static StringBuilder getDetail(Document document) {
		StringBuilder sb = new StringBuilder();
		Elements elements = document.getElementsByTag("script");
		for (int i = 0; i<elements.size(); i++) {
			Element element = elements.get(i);
			if (element.data() != null && element.data().contains("window._appState")) {
				String jsonStr = element.data().toString().replaceAll("window._appState = ", "").replace(";", "");
				JSONObject jsonObject = JSON.parseObject(jsonStr);
				JSONObject jsonContent = jsonObject.getJSONObject("detailInfo");
				JSONObject jsonPhoto = jsonObject.getJSONObject("photos");
//				System.out.println("id= " + jsonContent.get("poiId"));
//				System.out.println("标题= " + jsonContent.get("name"));
//				System.out.println("评分= " + jsonContent.get("avgScore"));
//				System.out.println("地址= " + jsonContent.get("address"));
//				System.out.println("营业时间= " + jsonContent.get("openTime"));
//				System.out.println("经度= " + jsonContent.get("longitude"));
//				System.out.println("纬度= " + jsonContent.get("latitude"));
//				System.out.println("电话= " + jsonContent.get("phone"));
//				System.out.println("评价价格= " + jsonContent.get("avgPrice"));
//				JSONObject jsonPhoto = jsonObject.getJSONObject("photos");
//				System.out.println("头像= " + jsonPhoto.get("frontImgUrl"));
//				JSONArray otherJsonArray = jsonPhoto.getJSONArray("albumImgUrls");
//				for (int j = 0; j<otherJsonArray.size(); j++) {
//					System.out.println("其他图片头像= " + otherJsonArray.getString(j));
//				}
				sb.append(jsonContent.get("poiId")).append("|").append(jsonContent.get("name")).append("|").append(jsonContent.get("longitude")).append("|").append(jsonContent.get("latitude"))
						.append("|").append(jsonPhoto.get("frontImgUrl")).append(jsonContent.get("phone"))
						.append("|").append(jsonContent.get("address")).append("\n");
			}
		}
		return sb;
	}


	public static void checkVerify(Document document, String url) {
		Elements checkElements = document.getElementsByClass("logo");
		for (int i = 0; i< checkElements.size(); i++) {
			if ("验证中心".equals(checkElements.get(i).ownText())) {
				System.out.println("当前访问到的地址为：" + url);
				throw new IllegalArgumentException("当前访问到的地址为：" + url + "页面需要输入验证码，请手动输入");
			}

		}
	}

	public static void writeFile(String content,String fileName) {
		File file = new File(writeFile + fileName);
		FileWriter fw=null;
		BufferedWriter bw=null;
		try{
			if(!file.exists()){
				file.createNewFile();
			}
			fw=new FileWriter(file.getAbsoluteFile(),true);  //true表示可以追加新内容
			bw=new BufferedWriter(fw);
			bw.write(content);
			bw.close();
		}catch(Exception e){
			e.printStackTrace();
		}

	}
}
